package cn.yunhe.action;

import cn.yunhe.busi.BookBusi;
import cn.yunhe.busi.impl.BookBusiImpl;
import cn.yunhe.model.Book;
import org.apache.struts2.ServletActionContext;
import util.BaseAction;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2018/4/13.
 */
public class AddBookAction extends BaseAction {
    private String isbn;
    private String name;
    private String author;
    private String publish;
    private int price;
    private int currcount;
    private String picture;

    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    public String execute() throws IOException {
        picture = "";
        if (upload !=null){
            picture = getSavePath()+"\\"+getUploadFileName();
            doUpload(upload, picture);
        }

        Book book = new Book(isbn,name,author,publish,price,currcount,picture);
        BookBusi bookBusi = new BookBusiImpl();
        boolean result = bookBusi.addBook(book);

        if (result) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    public void doUpload(File file,String uploadFilePath) throws IOException{
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(uploadFilePath);
        byte [] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        fis.close();
        fos.close();
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublish() {
        return publish;
    }

    public void setPublish(String publish) {
        this.publish = publish;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCurrcount() {
        return currcount;
    }

    public void setCurrcount(int currcount) {
        this.currcount = currcount;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }
}
